package br.com.iswe.recipemaster.gwt.recipe.client;

import br.com.iswe.gwt.ui.client.BeanMap;
import br.com.iswe.gwt.ui.client.CheckBoxBag;
import br.com.iswe.recipemaster.gwt.client.RmListPanel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;

public class RecipeListPanel extends RmListPanel {
	
	protected FlexTable flexTable;

	public RecipeListPanel(){
		flexTable = new FlexTable();
		initWidget(flexTable);
		
		//ID
		flexTable.setWidget(0, 1, createColumnHeader("ID", "recipeId"));
		flexTable.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
		
		//Name
		flexTable.setWidget(0, 2, createColumnHeader("Name", "name"));
		flexTable.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
		
		setupScreen();
	}
	
	public void setupScreen() {
		super.setupScreen();
	}
	
	protected FlexTable getFlexTable() {
		return flexTable;
	}
	
	protected void buildRow(int row, final BeanMap item) {
		if(isCheckBoxesEnabled()){
			//check box
			CheckBoxBag checkBox = new CheckBoxBag(item);
			checkBoxList.add(checkBox);
			flexTable.setWidget(row, 0, checkBox );
		}
		if(isLinkEnabled()){
			//Id
			Hyperlink link = new Hyperlink(item.getString("recipeId"), "DETAIL");
			link.addClickHandler(new ClickHandler(){
				public void onClick(ClickEvent event) {
					onSelectListener.fire(item);
				}
			});
			flexTable.setWidget(row, 1, link);
			
			//Name
			link = new Hyperlink(item.getString("name"), "DETAIL");
			link.addClickHandler(new ClickHandler(){
				public void onClick(ClickEvent event) {
					onSelectListener.fire(item);
				}
			});
			flexTable.setWidget(row, 2, link);
		}
		else{
			//Recipe
			flexTable.setWidget(row, 1, new Label( item.getString("recipeId") ));
			
			//Name
			flexTable.setWidget(row, 2, new Label( item.getString("name") ));
		}	
	}

}
